﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Windows.Controls;
using Xceed.Silverlight.Data;

namespace Xceed.Silverlight.ListBox
{
  internal class ScrollToIndexNavigationAction : PanelNavigationActionBase
  {
    #region Constructor

    internal ScrollToIndexNavigationAction( double scrollManagerInitialTargetIndex, PanelBase panel, NavigationInfo navigationInfo )
      : base( panel, navigationInfo )
    {
      m_initialTargetIndex = scrollManagerInitialTargetIndex;

      // Compare the index of first and last fully visible DataPath
      // to determine the real number of fully visible items in viewport
      // to avoid scrolling to much and skip containers.
      PanelBase.PanelLayoutManager layoutManager = this.Panel.LayoutManager;

      DataPath firstVisibleDataPath = layoutManager.GetFirstFullyVisibleDataPath();
      DataPath lastVisibleDataPath = layoutManager.GetLastFullyVisibleDataPath();

      int firstVisibleIndex = layoutManager.ContainerDescriptions.IndexOf( firstVisibleDataPath );
      int lastVisibleIndex = layoutManager.ContainerDescriptions.IndexOf( lastVisibleDataPath );

      // Try to get the last fully visible DataPath in the viewport
      // to know how many containers must be skipped during a page.
      // If the container size is very large and only 1 or 2 
      // are fully visible, ensure to at least scroll 1 index
      m_fullyVisibleItemsInViewport = Math.Max( 1d, lastVisibleIndex - firstVisibleIndex );
    }

    #endregion

    internal override double CalculateTargetScrollIndex()
    {
      double targetIndex = m_initialTargetIndex;
      double directionalScrollingFactor = ( this.NavigationInfo.IsForwardScrolling ) ? 1 : -1;

      if( this.NavigationInfo.IsPageScrolling )
      {
        targetIndex += m_fullyVisibleItemsInViewport * directionalScrollingFactor;
      }
      else
      {
        targetIndex += directionalScrollingFactor;
      }

      if( targetIndex <= 0d )
        return Math.Max( 0d, targetIndex );

      PanelBase panel = this.Panel;
      double maxTargetIndex;

      if( panel.GetScrollingOrientation() == Orientation.Vertical )
      {
        maxTargetIndex = Math.Max( 0d, panel.ExtentHeight - panel.ViewportHeight );
      }
      else
      {
        maxTargetIndex = Math.Max( 0d, panel.ExtentWidth - panel.ViewportWidth );
      }

      return Math.Min( targetIndex, maxTargetIndex );
    }

    #region Private Fields

    private readonly double m_initialTargetIndex;
    private readonly double m_fullyVisibleItemsInViewport;

    #endregion
  }
}
